package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WcsPcedit;
import com.ruoyi.system.service.IWcsPceditService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据录入Controller
 * 
 * @author ZYT
 * @date 2020-11-27
 */
@RestController
@RequestMapping("/system/WcsPcedit")
public class WcsPceditController extends BaseController
{
    @Autowired
    private IWcsPceditService wcsPceditService;

    /**
     * 查询数据录入列表
     */
    @PreAuthorize("@ss.hasPermi('system:WcsPcedit:list')")
    @GetMapping("/list")
    public TableDataInfo list(WcsPcedit wcsPcedit)
    {
        startPage();
        List<WcsPcedit> list;
        if(wcsPcedit.getSpearId()==null){
            list=new ArrayList<>();
        }else {
            list = wcsPceditService.selectWcsPceditList(wcsPcedit);
        }
        return getDataTable(list);
    }

    /**
     * 导出数据录入列表
     */
    @PreAuthorize("@ss.hasPermi('system:WcsPcedit:export')")
    @Log(title = "数据录入", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WcsPcedit wcsPcedit)
    {
        List<WcsPcedit> list = wcsPceditService.selectWcsPceditList(wcsPcedit);
        ExcelUtil<WcsPcedit> util = new ExcelUtil<WcsPcedit>(WcsPcedit.class);
        return util.exportExcel(list, "WcsPcedit");
    }

    /**
     * 获取数据录入详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:WcsPcedit:query')")
    @GetMapping(value = "/{trkId}")
    public AjaxResult getInfo(@PathVariable("trkId") Long trkId)
    {
        return AjaxResult.success(wcsPceditService.selectWcsPceditById(trkId));
    }

    /**
     * 新增数据录入
     */
    @PreAuthorize("@ss.hasPermi('system:WcsPcedit:add')")
    @Log(title = "数据录入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WcsPcedit wcsPcedit)
    {
        if(StringUtils.isEmpty(wcsPcedit.getSpearId())){
            return AjaxResult.success("扫码器编号不能为空",101);
        }
        if(StringUtils.isEmpty(wcsPcedit.getContNo())){
            return AjaxResult.success("商品条码不能为空",101);
        }
        WcsPcedit wcsPceditRecord = wcsPceditService.selectWcsPceditBySpearId(wcsPcedit.getContNo());
        //判断是否可以录入重复值
//        if(wcsPceditRecord!=null && StringUtils.equals("1",wcsPceditRecord.getDealFlag())){
//            return AjaxResult.success("此包裹号:"+wcsPceditRecord.getContNo() +"已存在,请异常处理",101);
//        }
        if(wcsPceditRecord!=null && StringUtils.equals("2",wcsPceditRecord.getDealFlag())){
            return AjaxResult.success("此包裹号:"+wcsPceditRecord.getContNo() +"WMS无任务,请异常处理",101);
        }
        return toAjax(wcsPceditService.insertWcsPcedit(wcsPcedit));
    }

    /**
     * 修改数据录入
     */
    @PreAuthorize("@ss.hasPermi('system:WcsPcedit:edit')")
    @Log(title = "数据录入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WcsPcedit wcsPcedit)
    {
        return toAjax(wcsPceditService.updateWcsPcedit(wcsPcedit));
    }

    /**
     * 删除数据录入
     */
    @PreAuthorize("@ss.hasPermi('system:WcsPcedit:remove')")
    @Log(title = "数据录入", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trkIds}")
    public AjaxResult remove(@PathVariable Long[] trkIds)
    {
        return toAjax(wcsPceditService.deleteWcsPceditByIds(trkIds));
    }

}
